<?php
declare(strict_types=1);
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2022/05/20
//      |_|                                         | TpMeCMS

namespace ImiApp\ApiServer\Api\Controller\com;

use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
// use ImiApp\ApiServer\Common\AuthApi;
use Phpben\Imi\Auth\Annotation\Auth;



use Imi\Db\Db;
use ImiApp\ApiServer\Api\Controller\Tpmecms;



/**
 * 常见问题文章🌵
 * @Auth(name="token",nologin={"login","register","resetpwd","test"})
 * @Controller("/com.artcle/")
 */
class Artcle extends Tpmecms
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];



    /**
     * 获取常见问题分类列表🌵
     *
     * @Action
     * @return void
     */
    public function get_artclecate()
    {

        $list = Db::name('artclecate')->fieldRaw('id,name,image')->select()->getArray();
        return $this->success('ok', $list);
    }

    /**
     * 获取常见问题列表🌵
     *
     * @param string $search 搜索
     * @param int $artclecate_id 分类id
     * @param integer $page 1
     * @param integer $limit 8
     * @Action
     * @return void
     */
    public function get_artcle_list(?int $artclecate_id = null,?int $page = 1,?int $limit = 8,?string $search = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 8;
        $data['switch'] = 1;
        if ($artclecate_id)
            $data['artclecate_id'] = $artclecate_id;
        if ($search)
            $data['name'] = ['like', "%" . $search . "%"];
            // $data['content']
        $list = Db::name('artcle')->whereEx($data)->orderRaw('weigh desc,createtime desc')->page($page, $limit)->fieldRaw('id,name,artclecate_id,weigh,createtime')->select()->getArray();
        if ($list)
            foreach ($list as $key => &$val) {
                $this->ChangeFieldData($val);
            }
        return $this->success('ok', $list);
    }

    /**
     * 获取常见问题详情🌵
     *
     * @param int $id 公告id
     * @Action
     * @return void
     */
    public function get_artcle_data($id = null)
    {
        $data['switch'] = 1;
        $data['id'] = $id;

        $res = Db::name('artcle')->whereEx($data)->find();
        if (!$res) {
            return $this->error('没有该信息');
        }
        $this->ChangeFieldData($res);
        $this->AddViews($id);

        return $this->success('ok', $res);
    }

    /**
     * 增加浏览量
     *
     * @param int $id
     * @return void
     */
    protected function AddViews($id)
    {
        Db::name('artcle')->where('id','=', $id)->setFieldInc('views')->update();
        if ($this->auth->isLogin())
            $this->AddViewlog(ARTCLE, $id, $this->auth->user()->id);
    }



    /**
     * 改变通知字段
     *
     * @param array $val 
     * @return void
     */
    protected function ChangeFieldData(&$val)
    {
        $val['cate'] = Db::name('artclecate')->where('id', '=',$val['artclecate_id'])->value('name');
        $val['date_str'] = $this->TimeToStr($val['createtime']);
        return $val;
    }
}
